package com.oywy.rbac.controller;

import com.oywy.rbac.entity.Role;
import com.oywy.rbac.service.RoleService;
import com.oywy.rbac.vo.ApiDataGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 *
 * @author oywy
 * @date 2018/10/22
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping
    public ApiDataGridResult<Role> list() {
        List<Role> roles = roleService.list();
        return new ApiDataGridResult<>(roles.size(), roles);
    }

    @GetMapping("{id}")
    public Role get(@PathVariable String id) {
        return roleService.get(id);
    }

    @PostMapping
    public Role post(Role role) {
        return roleService.post(role);
    }

    @PostMapping("{id}/authority")
    public ResponseEntity grantAuthorities(@PathVariable("id") String id,@RequestParam("authorityIds") List<String> authorityIds) {
        roleService.grantAuthorities(id,authorityIds);
        return ResponseEntity.ok().build();
    }

    @PutMapping
    public Role put(Role role) {
        return roleService.put(role);
    }

    @DeleteMapping("{id}")
    public void del(@PathVariable String id) {
        roleService.del(id);
    }
}
